function [markup_t, negativemarkup_carrier_t] = invertcost_t(t, ind_mkt_start, ind_mkt_end, share, prob_i, ...
    sim_pcoeff, n_smln, carrier_ind, oem_ind, setup)

n_carrier = size(carrier_ind, 2);
n_oem = size(oem_ind, 2);

ind_t = ind_mkt_start(t):ind_mkt_end(t);
n_prod_t = length(ind_t);

share_t = share(ind_t, :);
simShare_t = prob_i(ind_t, :);

sim_pcoeff_t = sim_pcoeff(ind_t, :);
simSharep_t = simShare_t.*sim_pcoeff_t;

simSharep_t_weighted = simSharep_t / n_smln;

if isempty(carrier_ind)
    carrier_t = [];
else
    carrier_t = carrier_ind(ind_t, :);
end
    
oem_t = oem_ind(ind_t, :);


%% dsdp
dsdp = diag(sum(simSharep_t_weighted,2)) - (simSharep_t_weighted)*simShare_t'; 

%% markup
if isequal(setup.pricing_model, 'oem_carrier_collusion')
    collusionweightmatrix = zeros(n_prod_t, n_prod_t);
    for no = 1 : n_oem
        Jot = oem_t(:, no);
        for nc = 1 : n_carrier
            Jct = carrier_t(:, nc);
            collusionweightmatrix(Jot&Jct, Jot&Jct) = 1;
            collusionweightmatrix(Jot&Jct, Jot&~Jct) = setup.collusion_para_oem * setup.collusion_para_oem_share_of_profit; 
            collusionweightmatrix(Jot&Jct, ~Jot&Jct) = setup.collusion_para_carrier * (1-setup.collusion_para_oem_share_of_profit);
        end
    end
end
    
negativemarkup_carrier_t = nan(n_prod_t,1);

if isequal(setup.pricing_model, 'direct_oem')
    for no = 1 : n_oem
        Jot = oem_t(:, no)>0;
        negativemarkup_carrier_t(Jot) = dsdp(Jot, Jot)'\share_t(Jot, :);
    end
    markup_t = -negativemarkup_carrier_t;
    negativemarkup_carrier_t = zeros(n_prod_t,1);
elseif isequal(setup.pricing_model, 'direct_carrier')
    for nc = 1 : n_carrier
        Jct = carrier_t(:, nc);
        negativemarkup_carrier_t(Jct) = dsdp(Jct, Jct)'\share_t(Jct, :);
    end
    markup_t = zeros(n_prod_t, 1);
elseif isequal(setup.pricing_model, 'oem_carrier_collusion')
    negativemarkup_carrier_t = setup.collusion_para_oem_share_of_profit*((collusionweightmatrix .* dsdp')\share_t);
    markup_t = -(1-setup.collusion_para_oem_share_of_profit) * negativemarkup_carrier_t;
            
elseif isequal(setup.pricing_model, 'sequential')
    
    for nc = 1 : n_carrier
        Jct = carrier_t(:, nc);
        negativemarkup_carrier_t(Jct) = dsdp(Jct, Jct)'\share_t(Jct, :);
    end
    

    %% ds2dp2: ds2dp2(j,i,k) is ds_j^2/dp_i*dp_k. 
    Esjsisk = simSharep_t_weighted(unique_ind_ijk1(:, 1), :).*simSharep_t(unique_ind_ijk1(:, 2), :);
    Esjsisk = sum(Esjsisk(recover_ind_ijk1, :).*simShare_t(unique_ind_ijk2, :), 2);

    ds2dp2 = nan(n_prod_t, n_prod_t, n_prod_t);
    for k = 1 : n_prod_t
        ds2dp2k = zeros(n_prod_t, n_prod_t);
        ds2dp2k(k,k) = -sum(sim_pcoeff_t(k, :).*simSharep_t_weighted(k,:),2); % -(i==j)*(j==k)*simShare_t(j, :)
        ds2dp2k(:,k) = ds2dp2k(:,k) + (simSharep_t_weighted * simSharep_t(k,:)'); % +(i==k)*simShare_t(i, :).*simShare_t(j, :)
        ds2dp2k(k,:) = ds2dp2k(k,:) + (simSharep_t(k,:) * simSharep_t_weighted'); % +(j==k)*simShare_t(i, :).*simShare_t(j, :)
        ds2dp2k = ds2dp2k + diag((simSharep_t(k, :)*simSharep_t_weighted')); % + (i==j)*simShare_t(k, :).*simShare_t(j, :)
        ds2dp2(:,:,k) = ds2dp2k;
    end

    ds2dp2 = -ds2dp2+2*reshape(Esjsisk(recover_ind_ijk2), [n_prod_t n_prod_t n_prod_t]);
    
    %% compute dpdw
    D0 = nan(n_prod_t, n_prod_t);
    
    for i = 1 : n_prod_t
        Jct = carrier_t(i,:); % find the models of the same carrier as i
        Jct = carrier_t(:,Jct);
        tmp = squeeze(ds2dp2(Jct, i, :));
        D0(i,:) = -negativemarkup_carrier_t(Jct)'*tmp;
        D0(i,Jct) = D0(i,Jct) + dsdp(Jct, i)';
    end
    
    D0 = D0 + dsdp;

    same_carrier = false(n_prod_t, n_prod_t);
    
    for nc = 1 : n_carrier
        Jct = carrier_t(:, nc);
        same_carrier(Jct, Jct) = true;
    end
    
    dpdw = D0\(dsdp'.*same_carrier); % (k,h)-element = dp_kdw_h
    dsdw = dsdp*dpdw; % (j,i)-element = ds_jdw_i
    
    % recover the marginal cost of the phone maker
    
    markup_t = nan(n_prod_t, 1);
    for no = 1 : n_oem
        Jmt = oem_t(:, no);
        if any(Jmt)
            markup_t(Jmt, :) = - inv(dsdw(Jmt, Jmt)')*share_t(Jmt);
        end
    end
 
else
    error('unknown pricing model');
end
    